package com.babyqiao.wallpaper.manager.controller;


import com.babyqiao.wallpaper.common.UnifyResult;
import com.babyqiao.wallpaper.manager.entity.WallpaperCmsSwiper;
import com.babyqiao.wallpaper.manager.service.CmsSwiperService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

/**
 * @author QLZ
 * @date 2020/12/22
 */
@RestController
@RequestMapping("/api/cms/swiper")
@Api(tags = "轮播图后台管理接口")
public class SwiperAdminController {

    @Autowired
    private CmsSwiperService cmsSwiperService;


    /**
     * 分页查询轮播图
     *
     * @param current 当前页数
     * @param limit   每页数量
     * @return result轮播图 total数据总数
     */
    @ApiOperation(value = "分页查询轮播图", httpMethod = "GET")
    @GetMapping("/{current}/{limit}/swipers")
    public UnifyResult getSwiper(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable long current,
                                 @ApiParam(name = "limit", value = "每页查询条数", required = true) @PathVariable long limit) {
        Page<WallpaperCmsSwiper> page = new Page<>(current, limit);
        cmsSwiperService.page(page, null);
        long total = page.getTotal();
        return UnifyResult.success().data("result", page.getRecords()).data("total", total);
    }

    /**
     * 根据id查询轮播图
     *
     * @param id 轮播图id
     * @return 轮播图信息
     */
    @ApiOperation(value = "根据ID查询轮播图", httpMethod = "GET")
    @GetMapping("/{id}/swiper")
    public UnifyResult getById(@ApiParam(name = "id", value = "轮播图id", required = true) @PathVariable String id) {
        WallpaperCmsSwiper byId = cmsSwiperService.getById(id);
        return UnifyResult.success().data("result", byId);
    }

    /**
     * 添加轮播图
     *
     * @param wallpaperCmsSwiper 轮播图参数
     * @return 是否成功
     */
    @ApiOperation(value = "添加轮播图", httpMethod = "POST")
    @PostMapping("/swiper")
    @Caching(evict = {
            @CacheEvict(value = "HomeContent",key = "'HomeContent'")
    })
    public UnifyResult addSwiper(@ApiParam(name = "wallpaperCmsSwiper", value = "添加轮播图的参数", required = true) @RequestBody WallpaperCmsSwiper wallpaperCmsSwiper) {
        boolean save = cmsSwiperService.save(wallpaperCmsSwiper);
        if (save) {
            return UnifyResult.success();
        }
        return UnifyResult.error();

    }

    /**
     * 删除轮播图
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除轮播图", httpMethod = "DELETE")
    @DeleteMapping("/{id}/swiper")
    @Caching(evict = {
            @CacheEvict(value = "HomeContent",key = "'HomeContent'")
    })
    public UnifyResult deleteSwiper(@PathVariable String id) {
        boolean b = cmsSwiperService.removeById(id);
        if (b) {
            return UnifyResult.success();
        }
        return UnifyResult.error();

    }

    @ApiOperation(value = "修改轮播图", httpMethod = "PUT")
    @PutMapping("/swiper")
    @Caching(evict = {
            @CacheEvict(value = "HomeContent",key = "'HomeContent'")
    })
    public UnifyResult updateSwiper(@RequestBody WallpaperCmsSwiper wallpaperCmsSwiper) {
        boolean b = cmsSwiperService.updateById(wallpaperCmsSwiper);
        if (b) {
            return UnifyResult.success();
        }
        return UnifyResult.error();
    }
}
